package com.hgm.study.ui.components


import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.foundation.text.appendInlineContent
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.HelpOutline
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.Placeholder
import androidx.compose.ui.text.PlaceholderVerticalAlign
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun DailyTaskContent() {

      DailyTaskItem(
            title = "登录",
            secondaryText = "5积分/每日首次登录",
            desc = "已或5积分/每日上限5积分",
            percent = 1f
      )

      DailyTaskItem(
            title = "文章学习",
            secondaryText = "10积分/每有效阅读1篇文章",
            desc = "已或50积分/每日上限100积分",
            percent = 0.7f
      )

      DailyTaskItem(
            title = "视听学习",
            secondaryText = "10积分/每有效观看视频或收听音频累计",
            desc = "已或50积分/每日上限100积分",
            percent = 0.3f
      )
}


@Composable
fun DailyTaskItem(title: String, secondaryText: String, desc: String, percent: Float) {
      val inlineContentId = "inlineContentId"

      val secondaryAnnotatedText = buildAnnotatedString {
            append(secondaryText)
            appendInlineContent(inlineContentId, "[icon]")
      }

      val inlineContent = mapOf(
            Pair(inlineContentId,
                  InlineTextContent(
                        Placeholder(
                              width = 14.sp,
                              height = 14.sp,
                              placeholderVerticalAlign = PlaceholderVerticalAlign.AboveBaseline
                        )
                  ) {
                        Icon(
                              imageVector = Icons.Default.HelpOutline,
                              contentDescription = null,
                              modifier = Modifier.clickable { })
                  }
            )
      )


      Row(
            modifier = Modifier.fillMaxWidth().padding(vertical = 8.dp),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
      ) {
            Column(modifier = Modifier.weight(7.5f)) {
                  Text(
                        text = title,
                        fontSize = 16.sp,
                        color = Color(0xFF333333),
                        fontWeight = FontWeight.W700
                  )

                  Text(
                        text = secondaryAnnotatedText,
                        inlineContent = inlineContent,
                        fontSize = 14.sp,
                        color = Color(0xFF333333),
                        fontWeight = FontWeight.W600
                  )

                  Row(verticalAlignment = Alignment.CenterVertically) {
                        LinearProgressIndicator(progress = percent, modifier = Modifier.weight(3f))
                        Text(
                              text = desc,
                              fontSize = 12.sp,
                              color = Color(0xFF333333),
                              modifier = Modifier
                                    .weight(7f, false)
                                    .padding(start = 8.dp)
                        )
                  }
            }

            //按钮
            OutlinedButton(
                  onClick = {},
                  border = if (percent >= 1f) ButtonDefaults.outlinedBorder else BorderStroke(
                        width = 1.dp,
                        color = Color(0xFFFF5900)
                  ),
                  colors = ButtonDefaults.outlinedButtonColors(contentColor = Color(0xFFFF5900)),
                  shape = CircleShape,
                  modifier = Modifier.weight(2.5f),
                  enabled = percent < 1f
            ) {
                  Text(text = "去学习")
            }
      }
}



